package api

import (
	"encoding/json"
	"errors"
	"fmt"
	"hft/tools/weixin"
	"hft/tools/weixin/model"
	"net/url"

	"hft/e"

	"github.com/tidwall/gjson"
)

type Union struct{}

// 换取unionid
func (*Union) GetUnionID(open_id string) (union string, err error) {
	fmt.Println("union", open_id)
	data := weixin.WeiXinBase.BaseRequestV2("GET", "/cgi-bin/user/info", &url.Values{
		"openid": []string{open_id},
	}, []byte{})
	var res = string(data)
	fmt.Println(res)
	errcode := gjson.Get(res, "errcode").String()
	if errcode != "" {
		return "", errors.New(e.ErrorInfo)
	}
	var unionModel *model.GetUnionModel
	err = json.Unmarshal(data, &unionModel)
	if err != nil {
		return "", errors.New(e.ErrorInfo)
	}
	return unionModel.UnionID, nil
}

var WeiXinUnion = new(Union)
